<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Repositories\ProductRepo;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Util\Sku;



class ProductController extends BaseController
{
    public $repo;


    public function _initialize()
    {
        $this->repo = new ProductRepo;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        $pageSize =  $request->pageSize ? $request->pageSize : 10;
        $products = $this->repo->getProductList($where,$pageSize);
        return $this->sucRes(200,$products);
    }

    public function del(Request $request)
    {
        $ids =  $request->ids;
        $this->repo->delProducts($ids);
        return $this->sucRes(200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $input = $request->only(['name','cate_id','cate_name','tip','price','store','image','desc','content','spec_type','attr','skus']);

        var_dump($input);
        exit;
        $imageList = $request->imageList;
        $specList  = $request->specLst;
        $this->repo->addProduct($input,$imageList,$specList);
        return $this->sucRes(200,$input);
    }

    public function update(Request $request)
    {
        $id = $request->id;
        $input = $request->only(['cate_id','cate_name','tip','name','image','price','store','desc','content','spec_type','skus','attr']);
        $imageList = $request->images;
        $specList  = $request->specLst;
        
        $this->repo->editProduct($id,$input,$imageList,$specList);
        return $this->sucRes();
    }
    public function info(Request $request)
    {
        $id = $request->id;
        $data = $this->repo->getOneProduct($id);
        return $this->sucRes(200,$data);
    }

    public function setup(Request $request)
    {
        $id = $request->id;
        $this->repo->setUp($id);
        return $this->sucRes();
    }
    public function setdown(Request $request)
    {
        $id = $request->id;
        $this->repo->setDown($id);
        return $this->sucRes();
    }
    public function recommend(Request $request)
    {
        $id = $request->id;
        $this->repo->setRecommend($id);
        return $this->sucRes();
    }
    public function unrecommend(Request $request)
    {
        $id = $request->id;
        $this->repo->setUnRecommend($id);
        return $this->sucRes();
    }
    public function createSku(Request $request) 
    {
        $skus = $request->skus;
        $sku = new Sku;
        $data = $sku->generate($skus);
        $response = [];
        foreach ($data as $value) {
            $response[]['spec'] = $value;    
        }
        return $this->sucRes(200,$response); 
    }
    // 以下为测试

    public function test(Request $request) 
    {
        //测试规格
        $skus = $request->skus;
        $sku = new \App\Util\Sku;
        return $sku->generate($skus);
    }
}